home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gscscie.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.5 KB  |  350 lines

  1. /* Copyright (C) 1998, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gscscie.c,v 1.3 2000/09/19 19:00:27 lpd Exp $ */
  20. /* CIE color space management */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gsmatrix.h"        /* for gscolor2.h */
  26. #include "gxcspace.h"
  27. #include "gscolor2.h"        /* for gs_set/currentcolorrendering */
  28. #include "gxcie.h"
  29. #include "gxarith.h"
  30. #include "gxdevice.h"        /* for gxcmap.h */
  31. #include "gxcmap.h"
  32. #include "gzstate.h"
  33.  
  34. /* ---------------- Color space definition ---------------- */
  35.  
  36. /* GC descriptors */
  37. private_st_cie_common();
  38. private_st_cie_common_elements();
  39. private_st_cie_a();
  40. private_st_cie_abc();
  41. private_st_cie_def();
  42. private_st_cie_defg();
  43.  
  44. /* Define the CIE color space types. */
  45. private cs_proc_concrete_space(gx_concrete_space_CIE);
  46. private cs_proc_install_cspace(gx_install_CIE);
  47.  
  48. /* CIEBasedDEFG */
  49. gs_private_st_ptrs1(st_color_space_CIEDEFG, gs_base_color_space,
  50.      "gs_color_space(CIEDEFG)", cs_CIEDEFG_enum_ptrs, cs_CIEDEFG_reloc_ptrs,
  51.             params.defg);
  52. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEDEFG);
  53. const gs_color_space_type gs_color_space_type_CIEDEFG = {
  54.     gs_color_space_index_CIEDEFG, true, true,
  55.     &st_color_space_CIEDEFG, gx_num_components_4,
  56.     gx_no_base_space, gx_cspace_not_equal,
  57.     gx_init_CIE, gx_restrict_CIEDEFG,
  58.     gx_concrete_space_CIE,
  59.     gx_concretize_CIEDEFG, NULL,
  60.     gx_default_remap_color, gx_install_CIE,
  61.     gx_adjust_cspace_CIEDEFG, gx_no_adjust_color_count
  62. };
  63.  
  64. /* CIEBasedDEF */
  65. gs_private_st_ptrs1(st_color_space_CIEDEF, gs_base_color_space,
  66.     "gs_color_space(CIEDEF)", cs_CIEDEF_enum_ptrs, cs_CIEDEF_reloc_ptrs,
  67.             params.def);
  68. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEDEF);
  69. const gs_color_space_type gs_color_space_type_CIEDEF = {
  70.     gs_color_space_index_CIEDEF, true, true,
  71.     &st_color_space_CIEDEF, gx_num_components_3,
  72.     gx_no_base_space, gx_cspace_not_equal,
  73.     gx_init_CIE, gx_restrict_CIEDEF,
  74.     gx_concrete_space_CIE,
  75.     gx_concretize_CIEDEF, NULL,
  76.     gx_default_remap_color, gx_install_CIE,
  77.     gx_adjust_cspace_CIEDEF, gx_no_adjust_color_count
  78. };
  79.  
  80. /* CIEBasedABC */
  81. gs_private_st_ptrs1(st_color_space_CIEABC, gs_base_color_space,
  82.     "gs_color_space(CIEABC)", cs_CIEABC_enum_ptrs, cs_CIEABC_reloc_ptrs,
  83.             params.abc);
  84. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEABC);
  85. const gs_color_space_type gs_color_space_type_CIEABC = {
  86.     gs_color_space_index_CIEABC, true, true,
  87.     &st_color_space_CIEABC, gx_num_components_3,
  88.     gx_no_base_space, gx_cspace_not_equal,
  89.     gx_init_CIE, gx_restrict_CIEABC,
  90.     gx_concrete_space_CIE,
  91.     gx_concretize_CIEABC, NULL,
  92.     gx_remap_CIEABC, gx_install_CIE,
  93.     gx_adjust_cspace_CIEABC, gx_no_adjust_color_count
  94. };
  95.  
  96. /* CIEBasedA */
  97. gs_private_st_ptrs1(st_color_space_CIEA, gs_base_color_space,
  98.           "gs_color_space(CIEA)", cs_CIEA_enum_ptrs, cs_CIEA_reloc_ptrs,
  99.             params.a);
  100. private cs_proc_adjust_cspace_count(gx_adjust_cspace_CIEA);
  101. const gs_color_space_type gs_color_space_type_CIEA = {
  102.     gs_color_space_index_CIEA, true, true,
  103.     &st_color_space_CIEA, gx_num_components_1,
  104.     gx_no_base_space, gx_cspace_not_equal,
  105.     gx_init_CIE, gx_restrict_CIEA,
  106.     gx_concrete_space_CIE,
  107.     gx_concretize_CIEA, NULL,
  108.     gx_default_remap_color, gx_install_CIE,
  109.     gx_adjust_cspace_CIEA, gx_no_adjust_color_count
  110. };
  111.  
  112. /* Determine the concrete space underlying a CIEBased space. */
  113. private const gs_color_space *
  114. gx_concrete_space_CIE(const gs_color_space * pcs, const gs_imager_state * pis)
  115. {
  116.     const gs_cie_render *pcie = pis->cie_render;
  117.  
  118.     if (pcie == 0 || pcie->RenderTable.lookup.table == 0 ||
  119.     pcie->RenderTable.lookup.m == 3
  120.     )
  121.     return gs_cspace_DeviceRGB(pis);
  122.     else            /* pcie->RenderTable.lookup.m == 4 */
  123.     return gs_cspace_DeviceCMYK(pis);
  124. }
  125.  
  126. /* Install a CIE space in the graphics state. */
  127. /* We go through an extra level of procedure so that */
  128. /* interpreters can substitute their own installer. */
  129. private int
  130. gx_install_CIE(const gs_color_space * pcs, gs_state * pgs)
  131. {
  132.     return (*pcs->params.a->common.install_cspace) (pcs, pgs);
  133. }
  134.  
  135. /* Adjust reference counts for a CIE color space */
  136. private void
  137. gx_adjust_cspace_CIEDEFG(const gs_color_space * pcs, int delta)
  138. {
  139.     rc_adjust_const(pcs->params.defg, delta, "gx_adjust_cspace_CIEDEFG");
  140. }
  141.  
  142. private void
  143. gx_adjust_cspace_CIEDEF(const gs_color_space * pcs, int delta)
  144. {
  145.     rc_adjust_const(pcs->params.def, delta, "gx_adjust_cspace_CIEDEF");
  146. }
  147.  
  148. private void
  149. gx_adjust_cspace_CIEABC(const gs_color_space * pcs, int delta)
  150. {
  151.     rc_adjust_const(pcs->params.abc, delta, "gx_adjust_cspace_CIEABC");
  152. }
  153.  
  154. private void
  155. gx_adjust_cspace_CIEA(const gs_color_space * pcs, int delta)
  156. {
  157.     rc_adjust_const(pcs->params.a, delta, "gx_adjust_cspace_CIEA");
  158. }
  159.  
  160. /* ---------------- Procedures ---------------- */
  161.  
  162. /* ------ Internal initializers ------ */
  163.  
  164. /*
  165.  * Set up the default values for the CIE parameters that are common to
  166.  * all CIE color spaces.
  167.  *
  168.  * There is no default for the white point, so it is set equal to the
  169.  * black point. If anyone actually uses the color space in that form,
  170.  * the results are likely to be unsatisfactory.
  171.  */
  172. private void
  173. set_common_cie_defaults(gs_cie_common * pcommon, void *client_data)
  174. {
  175.     pcommon->RangeLMN = Range3_default;
  176.     pcommon->DecodeLMN = DecodeLMN_default;
  177.     pcommon->MatrixLMN = Matrix3_default;
  178.     pcommon->points.WhitePoint = BlackPoint_default;
  179.     pcommon->points.BlackPoint = BlackPoint_default;
  180.     pcommon->client_data = client_data;
  181. }
  182.  
  183. /*
  184.  * Set defaults for a CIEBasedABC color space.  This is also used for
  185.  * CIEBasedDEF and CIEBasedDEFG color spaces.
  186.  */
  187. private void
  188. set_cie_abc_defaults(gs_cie_abc * pabc, void *client_data)
  189. {
  190.     set_common_cie_defaults(&pabc->common, client_data);
  191.     pabc->RangeABC = Range3_default;
  192.     pabc->DecodeABC = DecodeABC_default;
  193.     pabc->MatrixABC = Matrix3_default;
  194. }
  195.  
  196. /*
  197.  * Set up a default color lookup table for a CIEBasedDEF[G] space. There is
  198.  * no specified default for this structure, so the values used here (aside
  199.  * from the input and output component numbers) are intended only to make
  200.  * the system fail in a predictable manner.
  201.  */
  202. private void
  203. set_ctbl_defaults(gx_color_lookup_table * plktblp, int num_comps)
  204. {
  205.     int i;
  206.  
  207.     plktblp->n = num_comps;
  208.     plktblp->m = 3;        /* always output CIE ABC */
  209.     for (i = 0; i < countof(plktblp->dims); i++)
  210.     plktblp->dims[i] = 0;
  211.     plktblp->table = 0;
  212. }
  213.  
  214. /*
  215.  * Allocate a color space and its parameter structure.
  216.  * Return 0 if VMerror, otherwise the parameter structure.
  217.  */
  218. private void *
  219. build_cie_space(gs_color_space ** ppcspace,
  220.         const gs_color_space_type * pcstype,
  221.         gs_memory_type_ptr_t stype, gs_memory_t * pmem)
  222. {
  223.     gs_color_space *pcspace;
  224.     int code = gs_cspace_alloc(&pcspace, pcstype, pmem);
  225.     gs_cie_common_elements_t *pdata;
  226.  
  227.     if (code < 0)
  228.     return 0;
  229.     rc_alloc_struct_1(pdata, gs_cie_common_elements_t, stype, pmem,
  230.               {
  231.               gs_free_object(pmem, pcspace, "build_cie_space");
  232.               return 0;
  233.               }
  234.               ,
  235.               "build_cie_space(data)");
  236.     *ppcspace = pcspace;
  237.     return (void *)pdata;
  238. }
  239.  
  240. /* ------ Constructors ------ */
  241.  
  242. int
  243. gs_cspace_build_CIEA(gs_color_space ** ppcspace, void *client_data,
  244.              gs_memory_t * pmem)
  245. {
  246.     gs_cie_a *pciea =
  247.     build_cie_space(ppcspace, &gs_color_space_type_CIEA, &st_cie_a, pmem);
  248.  
  249.     if (pciea == 0)
  250.     return_error(gs_error_VMerror);
  251.  
  252.     set_common_cie_defaults(&pciea->common, client_data);
  253.     pciea->common.install_cspace = gx_install_CIEA;
  254.     pciea->RangeA = RangeA_default;
  255.     pciea->DecodeA = DecodeA_default;
  256.     pciea->MatrixA = MatrixA_default;
  257.  
  258.     (*ppcspace)->params.a = pciea;
  259.     return 0;
  260. }
  261.  
  262. int
  263. gs_cspace_build_CIEABC(gs_color_space ** ppcspace, void *client_data,
  264.                gs_memory_t * pmem)
  265. {
  266.     gs_cie_abc *pabc =
  267.     build_cie_space(ppcspace, &gs_color_space_type_CIEABC, &st_cie_abc,
  268.             pmem);
  269.  
  270.     if (pabc == 0)
  271.     return_error(gs_error_VMerror);
  272.  
  273.     set_cie_abc_defaults(pabc, client_data);
  274.     pabc->common.install_cspace = gx_install_CIEABC;
  275.  
  276.     (*ppcspace)->params.abc = pabc;
  277.     return 0;
  278. }
  279.  
  280. int
  281. gs_cspace_build_CIEDEF(gs_color_space ** ppcspace, void *client_data,
  282.                gs_memory_t * pmem)
  283. {
  284.     gs_cie_def *pdef =
  285.     build_cie_space(ppcspace, &gs_color_space_type_CIEDEF, &st_cie_def,
  286.             pmem);
  287.  
  288.     if (pdef == 0)
  289.     return_error(gs_error_VMerror);
  290.  
  291.     set_cie_abc_defaults((gs_cie_abc *) pdef, client_data);
  292.     pdef->common.install_cspace = gx_install_CIEDEF;
  293.     pdef->RangeDEF = Range3_default;
  294.     pdef->DecodeDEF = DecodeDEF_default;
  295.     pdef->RangeHIJ = Range3_default;
  296.     set_ctbl_defaults(&pdef->Table, 3);
  297.  
  298.     (*ppcspace)->params.def = pdef;
  299.     return 0;
  300. }
  301.  
  302. int
  303. gs_cspace_build_CIEDEFG(gs_color_space ** ppcspace, void *client_data,
  304.             gs_memory_t * pmem)
  305. {
  306.     gs_cie_defg *pdefg =
  307.     build_cie_space(ppcspace, &gs_color_space_type_CIEDEFG, &st_cie_defg,
  308.             pmem);
  309.  
  310.     if (pdefg == 0)
  311.     return_error(gs_error_VMerror);
  312.  
  313.     set_cie_abc_defaults((gs_cie_abc *) pdefg, client_data);
  314.     pdefg->common.install_cspace = gx_install_CIEDEFG;
  315.     pdefg->RangeDEFG = Range4_default;
  316.     pdefg->DecodeDEFG = DecodeDEFG_default;
  317.     pdefg->RangeHIJK = Range4_default;
  318.     set_ctbl_defaults(&pdefg->Table, 4);
  319.  
  320.     (*ppcspace)->params.defg = pdefg;
  321.     return 0;
  322. }
  323.  
  324. /* ------ Accessors ------ */
  325.  
  326. int
  327. gs_cie_defx_set_lookup_table(gs_color_space * pcspace, int *pdims,
  328.                  const gs_const_string * ptable)
  329. {
  330.     gx_color_lookup_table *plktblp;
  331.  
  332.     switch (gs_color_space_get_index(pcspace)) {
  333.     case gs_color_space_index_CIEDEF:
  334.         plktblp = &pcspace->params.def->Table;
  335.         break;
  336.     case gs_color_space_index_CIEDEFG:
  337.         plktblp = &pcspace->params.defg->Table;
  338.         plktblp->dims[3] = pdims[3];
  339.         break;
  340.     default:
  341.         return_error(gs_error_rangecheck);
  342.     }
  343.  
  344.     plktblp->dims[0] = pdims[0];
  345.     plktblp->dims[1] = pdims[1];
  346.     plktblp->dims[2] = pdims[2];
  347.     plktblp->table = ptable;
  348.     return 0;
  349. }
  350.